<?php
class StarAction extends CommonAction {
	public function index(){
		$this->display();	
	}
	
	//添加明星html页面
	public function add(){
		$error = urldecode($_GET['error']);
		if($error){
			$this->assign('error',$error);	
		}
		$this->display();
	}
	
	//添加明星提交数据
	public function addSubmit(){
		$Star = D("Star"); // 实例化Star对象
		if (!$Star->create()){
			// 如果创建失败 表示验证没有通过 输出错误提示信息
			$this->redirect('Star/add',array('error' => urlencode($Star->getError())));
		}else{
			// 验证通过 如果上传头像了
			if($_FILES['photo']['name']){
				import('ORG.Net.UploadFile');
				$upload = new UploadFile();// 实例化上传类
				$upload->maxSize =  2097152;//文件大小2M
				$upload->savePath = './Public/Uploads/Star/';//保存路径建议与主文件平级目录或者平级目录的子目录来保存   
				$upload->saveRule = uniqid;//上传文件的文件名保存规则
				$upload->uploadReplace = true;//如果存在同名文件是否进行覆盖
				$upload->allowExts = array('jpg','jpeg','png','gif');//准许上传的文件类型
				$upload->allowTypes = array('image/png','image/jpg','image/jpeg','image/gif');//检测mime类型
				$upload->thumb = true;//是否开启图片文件缩略图
				$upload->thumbMaxWidth = '48,80,132,220';
				$upload->thumbMaxHeight = '48,80,132,220';
				$upload->thumbPrefix = '48_,80_,132_,220_';
				$upload->thumbRemoveOrigin = 1;//如果生成缩略图，是否删除原图

				if(!$upload->upload()) {// 上传错误提示错误信息
					$this->error($upload->getErrorMsg());
				}else{
					// 上传成功 获取上传文件信息
					$info =  $upload->getUploadFileInfo();				
					$data['photo'] = $info[0]['savename'];
				}	
			}
			import('ORG.Util.Input');
			$input = new Input();
			$data['area'] = $_POST['area'];
			$data['chinese_name'] = $input->addSlashes($_POST['chinese_name']);
			$data['sex'] = $_POST['sex'];
			$data['birthday'] = $_POST['year'].'-'.$_POST['month'].'-'.$_POST['day'];
			$data['constellation'] = $_POST['constellation'];
			$data['blood_type'] = $_POST['blood_type'];
			if($_POST['height']) $data['height'] = $_POST['height'];
			if($_POST['weight']) $data['weight'] = $_POST['weight'];
			if($_POST['bwh']) $data['bwh'] = $input->addSlashes($_POST['bwh']);
			if($_POST['weibo_address']) $data['weibo_address'] = $input->addSlashes($_POST['weibo_address']);
			if($_POST['intro']) $data['intro'] = $input->addSlashes($_POST['intro']);
			//print_r($data);exit;
			if($Star->add($data)){	
				$this->success('数据保存成功！');
			}else{
				$this->error($Star->getError());
			}
		}	
	}
	
	//明星列表
	public function starList(){
		$Star = D("Star");
		$list = $Star->order('create_datetime')->select();
		foreach($list as $key=>$row){
			//地域
			if($row['area']==1){
				$list[$key]['area'] = '大陆';	
			}elseif($row['area']==2){
				$list[$key]['area'] = '港台';
			}elseif($row['area']==3){
				$list[$key]['area'] = '日韩';
			}elseif($row['area']==4){
				$list[$key]['area'] = '其他';
			}
			//性别
			if($row['sex']==1){
				$list[$key]['sex'] = '女';	
			}else{
				$list[$key]['sex'] = '男';	
			}
			//生日
			$bir_arr = explode('-',$row['birthday']);
			if($bir_arr[1]<10){
				$bir_arr[1] = '0'.$bir_arr[1];	
			}
			if($bir_arr[2]<10){
				$bir_arr[2] = '0'.$bir_arr[2];	
			}
			$list[$key]['birthday'] = implode('-',$bir_arr);
			//头像
			if($row['photo']){
				$list[$key]['photo'] = '/Public/Uploads/Star/48_'.$row['photo'];	
			}
		}
		$this->assign('list',$list);
		$this->display();
	}
	
	//明星修改
	public function edit(){
		$id = intval($_GET['id']);
		$Star = M("Star");
		$list = $Star->where("id = $id")->find();
		if(empty($list)){
			$this->error($Star->getError());	
		}else{
			//生日
			$bir_arr = explode('-',$list['birthday']);
			$list['year'] = $bir_arr[0];
			if($bir_arr[1]<10){
				$list['month'] = $bir_arr[1];
				$list['month_html'] = '0'.$bir_arr[1];
			}else{
				$list['month'] = $bir_arr[1];
				$list['month_html'] = $bir_arr[1];				
			}
			if($bir_arr[2]<10){
				$list['day'] = $bir_arr[2];
				$list['day_html'] = '0'.$bir_arr[2];
			}else{
				$list['day'] = $bir_arr[2];
				$list['day_html'] = $bir_arr[2];				
			}
			//头像
			if($list['photo']){
				$list['photo'] = '/Public/Uploads/Star/48_'.$list['photo'];	
			}			
		}
		$this->assign('list',$list);
		$this->display();
	}
	
	//明星修改提交
	public function editSubmit(){
		$Star = D("Star"); // 实例化Star对象
		$validate = array(
			array('chinese_name','checkName','明星名已存在！',3,'function'), // 仅仅需要进行验证码的验证
		);
		$Star-> setProperty("_validate",$validate);		
		
		if (!$Star->create()){
			// 如果创建失败 表示验证没有通过 输出错误提示信息
			$this->error($Star->getError());
		}else{
			// 验证通过 如果上传头像了
			if($_FILES['photo']['name']){
				import('ORG.Net.UploadFile');
				$upload = new UploadFile();// 实例化上传类
				$upload->maxSize =  2097152;//文件大小2M
				$upload->savePath = './Public/Uploads/Star/';//保存路径建议与主文件平级目录或者平级目录的子目录来保存   
				$upload->saveRule = uniqid;//上传文件的文件名保存规则
				$upload->uploadReplace = true;//如果存在同名文件是否进行覆盖
				$upload->allowExts = array('jpg','jpeg','png','gif');//准许上传的文件类型
				$upload->allowTypes = array('image/png','image/jpg','image/jpeg','image/gif');//检测mime类型
				$upload->thumb = true;//是否开启图片文件缩略图
				$upload->thumbMaxWidth = '48,80,132,220';
				$upload->thumbMaxHeight = '48,80,132,220';
				$upload->thumbPrefix = '48_,80_,132_,220_';
				//$upload->thumbSuffix = '_48,_80,_130,_200';//缩略图文件后缀
				//$upload->thumbFile = 'linxinru,linxinru';//缩略图文件名
				$upload->thumbRemoveOrigin = 1;//如果生成缩略图，是否删除原图

				if(!$upload->upload()) {// 上传错误提示错误信息
					$this->error($upload->getErrorMsg());
				}else{
					// 上传成功 获取上传文件信息
					$info =  $upload->getUploadFileInfo();
					$data['photo'] = $info[0]['savename'];
				}	
			}
			import('ORG.Util.Input');
			$input = new Input();
			$data['id'] = $_POST['id'];
			$data['area'] = $_POST['area'];
			$data['chinese_name'] = $input->addSlashes($_POST['chinese_name']);
			$data['sex'] = $_POST['sex'];
			$data['birthday'] = $_POST['year'].'-'.$_POST['month'].'-'.$_POST['day'];
			$data['constellation'] = $_POST['constellation'];
			$data['blood_type'] = $_POST['blood_type'];
			if($_POST['height']) $data['height'] = $_POST['height'];
			if($_POST['weight']) $data['weight'] = $_POST['weight'];
			if($_POST['bwh']) $data['bwh'] = $input->addSlashes($_POST['bwh']);
			if($_POST['weibo_address']) $data['weibo_address'] = $input->addSlashes($_POST['weibo_address']);
			if($_POST['intro']) $data['intro'] = $input->addSlashes($_POST['intro']);
			//查询修改前图片删除
			$id = $_POST['id'];
			$oldPicPath = $Star->where("id=$id")->getField('photo');
			if( !empty($oldPicPath) && !empty($data['photo']) ){
				@unlink("./Public/Uploads/Star/48_".$oldPicPath);
				@unlink("./Public/Uploads/Star/80_".$oldPicPath);
				@unlink("./Public/Uploads/Star/132_".$oldPicPath);
				@unlink("./Public/Uploads/Star/220_".$oldPicPath);
            }

			if($Star->save($data)){	
				$this->success('修改成功！','starList');
			}else{
				$this->error($Star->getError());
			}			
		}
	}
	
	//禁用或者重新开启明星
	public function changeStatus(){
		$id = intval($_GET['id']);
		$status = intval($_GET['status']);
		if($status!=1 && $status!=2){
			$this->error('非法操作！','__APP__/Star/starList');	
		}else{
			$Star = M("Star");
			$row = $Star->where("id = $id")->find();
			$data['status'] = $status;
			$res = $Star->where("id = $id")->save($data);
			if($res){
				//$this->assign("jumpUrl","__APP__/User/userList"); 
				$this->success('修改成功！','__APP__/Star/starList');
			}else{
				$this->error($Admin->getError(),'__APP__/Star/starList');
			}
		}
	}	
	
	public function recomGoods($id){
		Load('extend');
		$StarId = $_GET['id'];

		$Goods = M('goods_base_info');
		$GoodsInfo = $Goods->where("goods_star_code=".$StarId)->select();
		
		$this->assign('starid',$StarId);
		$this->assign('goodsinfo',$GoodsInfo);
		$this->display();
	}

	public function addRecom(){
		import('ORG.Util.Mergy');
		$goodsIds = explode( ',' , trim( $_POST['goodArr'] , ',' ) );
		$starId = $_POST['starid'];
		//验证数据是否大于5个
		if( count($goodsIds)>3 ){
			echo '必须选择3个商品';
		}else{
			$goodsCode = trim( $_POST['goodArr'] , ',' );
		}
		$imageArr = array();
		//读取该明星的头像
		$row = '';
		$Star = M("Star");
		$row = $Star->where("id = $starId")->find();
		$imageArr[] = 'Public/Uploads/Star/220_'.$row['photo'];

		$Good = M('goods_base_info');
		$goodInfo = $Good->where("goods_code in ($goodsCode)")->select();
		//循环查询选择商品图片名称
		foreach($goodInfo as $key=>$val){
			$imageArr[] = 'Public/Uploads/Goods/72_'.$val['goods_photo_add'];
		}
		//生成明星同款图片
		$return = combine_image($imageArr,220,294,72,72,100,'Public/Uploads/StarSame/'.$row['id'].'.jpg');
		//入库操作
		$starSame = D('star_same');
		$data['star_id'] = $starId;
		$data['star_name'] = $row['chinese_name'];
		$data['goods_ids'] = $goodsCode;
		$data['pic_path'] = $row['id'].'.jpg';

		$sameInfo = $starSame->where("star_id=$starId")->select();
		//不存在同款信息新增
		if( empty($sameInfo) ){
			if ( false !== $starSame->add($data) ) {
				$this->assign("jumpUrl","recomGoods?id=$starId" );
				$this->success('操作成功！');
			} else {
				$this->assign("jumpUrl","recomGoods?id=$starId]" );
				$this->error('数据写入错误');
			}	
		}else{
			if ( false !== $starSame->where("star_id=$starId")->save($data) ) {
				$this->assign("jumpUrl","recomGoods?id=$starId" );
				$this->success('操作成功！');
			} else {
				$this->assign("jumpUrl","recomGoods?id=$starId]" );
				$this->error('数据写入错误');
			}
		}
	}
	
	
}

?>